# -*- coding: utf-8 -*-
# @Time    : 2024/1/16 20:18
# @Author  : micah
# @File    : 2.使用python保存字典数据到csv文件.py
# @Software: PyCharm


import csv


rows = [
    {
        "class_name": "18级Python",
        "name": '小王',
        "gender": '男',
        "phone": '13146060xx1',
        "qq": '123456xx1'
    },
    {
        "class_name": "18级Python",
        "name": '小李',
        "gender": '男',
        "phone": '13146060xx2',
        "qq": '123456xx2'
    },
    {
        "class_name": "19级Python",
        "name": '小赵',
        "gender": '女',
        "phone": '13146060xx3',
        "qq": '123456xx3'
    },
    {
        "class_name": "19级Python",
        "name": '小红',
        "gender": '女',
        "phone": '13146060xx4',
        "qq": '123456xx4'
    },
]

with open('stu_info_dict.csv', 'w', newline='') as file:
    # 1.创建csv写对象并声明表头信息
    f_csv = csv.DictWriter(file, ['class_name', 'name', 'gender', 'phone', 'qq'])

    # 2.将声明的表头信息写入到文件中
    f_csv.writeheader()

    # 3.写入学生信息
    f_csv.writerows(rows)



